﻿@using Orchard.ContentManagement;

@{
    Script.Require("jQuery").AtHead();
    
    var context = @WorkContext.CurrentSite.As<Mod.CookieDetector.Models.CookieDetectionSettingsPart>();
    
    var alert = context.AlertCookieBlockers;
    var first = context.TopMessage;
    var last = context.BottomMessage;
    var image = context.Image;
}

@Display(Model.Metadata.ChildContent)

@if(alert)
{
    <div id="cookies">
        <p>@Html.Raw(first)</p>
        <br />
        @if (image != "None")
        {
            <p style="text-align:center" ><img src='@image' alt="You have disabled cookies!" /></p>
        }
        <br />
        <p>@Html.Raw(last)</p>
    </div>

}

@using (Script.Foot())
{
    <script type="text/javascript">
        function createCookie(name, value, days) {
            var expires;
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = "; expires=" + date.toGMTString();
            }
            else expires = "";
            document.cookie = name + "=" + value + expires + "; path=/";
        }

        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }

        function eraseCookie(name) {
            createCookie(name, "", -1);
        }

        function areCookiesEnabled() {
            var r = false;
            createCookie("testing", "Hello", 1);
            if (readCookie("testing") != null) {
                r = true;
                eraseCookie("testing");
            }
            return r;
        }

        @if (alert == true)
        {
            <text>
            $(function () {
                if (!areCookiesEnabled()) {
                    $(".login-form").hide();
                    $("#login-man").hide();
                }
                else {
                    $("#cookies").hide();
                }
            });
            </text>
        }
    </script>
}